package jannex;


import jannex.mapping.Handlers;
import org.junit.Test;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class Tutorial {

    @Test
    public void basicSetup() throws SQLException {
        JannexBuilder builder = new JannexBuilder("jdbc:hsqldb:mem:mymemdb").credentials("SA", "");
        builder.binder(Vehicle.class).register();
//        builder.bind(Vehicle.class).table("vehicle").toBuilder()
//                .addAll(LabelMap.of(
//                        "id", "id",
//                        "wheels", "wheels",
//                        "color", "color"))
//                .addId("id").register();
//        builder.bind(Person.class).toBuilder().register();
//        builder.bind(Person.class).using(new Builder<Person>() {
//                @Override
//                public Person build() {
//                    return new Person();
//                }
//            });
//
        Jannex jannex = builder.build();
//
//        System.out.println(jannex.getMetaData().storesUpperCaseQuotedIdentifiers());
//        System.out.println(jannex.getMetaData().storesUpperCaseIdentifiers());
//
        jannex.query(
                "CREATE TABLE vehicle (\n" +
                        "    id bigint GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,\n" +
                        "    things int array,\n" +
                        "    wheels int NOT NULL,\n" +
                        "    color character varying(100) NOT NULL,\n" +
                        "    PRIMARY KEY (id)\n" +
                        ")"
        ).execute();

        jannex.query("INSERT INTO vehicle (things, wheels, color) VALUES (?, ?, ?)")
                .batch()
                .add(new int[] {1, 2, 3}, 2, "red")
                .add(new int[] {4, 5, 2}, 4, "blue")
                .execute();
        List<Map<String, Object>> result = jannex.query("SELECT * FROM vehicle").as(Handlers.map()).fetch().toList();
        System.out.println(result);

//        jannex.query(
//                "CREATE TABLE person (\n" +
//                        "    id bigint GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,\n" +
//                        "    name character varying(100) NOT NULL,\n" +
//                        "    email character varying(200) NOT NULL,\n" +
//                        "    PRIMARY KEY (id),\n" +
//                        "    UNIQUE (name)\n" +
//                        ")").execute();
//
//        jannex.query(
//                "CREATE TABLE rel_vehicle_person (\n" +
//                        "    vehicle_id bigint NOT NULL,\n" +
//                        "    person_id bigint NOT NULL,\n" +
//                        "    PRIMARY KEY (vehicle_id, person_id),\n" +
//                        "    FOREIGN KEY (vehicle_id) REFERENCES vehicles(id) ON UPDATE CASCADE ON DELETE CASCADE,\n" +
//                        "    FOREIGN KEY (person_id) REFERENCES persons(id) ON UPDATE CASCADE ON DELETE CASCADE\n" +
//                        ")").execute();
//
//
//        String sql = "INSERT INTO vehicles (id, wheels, color) VALUES (:id, :wheels, :color)";
//        final int result = jannex.query(sql).execute(1l, 2, "red");
//        Assert.assertEquals(1, result);
//
//        Map<String, Object> map = jannex.query("SELECT * FROM vehicles").as(Handlers.map()).fetch().first();
//        System.out.println(map);
//        List<Object> list = jannex.query("SELECT * FROM vehicles").as(Handlers.list()).fetch().first();
//        System.out.println(list);
//
//        ResultSetHandler<Vehicle> handler = new ResultSetHandler<Vehicle>() {
//            @Override
//            public Vehicle handle(JxResultSet resultSet) {
//                return new Vehicle(resultSet.getLong("id"), resultSet.getInt("wheels"), resultSet.getString("color"));
//            }
//
//            @Override
//            public void init(JxResultSetMetaData metaData) {
//            }
//        };
//        Vehicle vehicle = jannex.query("SELECT * FROM vehicles").as(handler).fetch().first();
//
//        System.out.println(vehicle);
//
//        try (TypedResultSet<Vehicle> resultSet = jannex.query("SELECT * FROM vehicles").as(handler).query()) {
//            for (Vehicle v : resultSet) {
//                System.out.println(v);
//            }
//        }

    }

}
